<?php
/*
 * This scaffolding is designed by Xrvel
 * http://www.xrvel.com/
 */

function xrvel_scaffolding_get_column_name($str) {
	if (strpos($str, '.') === false) {
		return $str;
	}
	$temp = explode('.', $str);
	return array_pop($temp);
}

function xrvel_scaffolding_get_primary_key($config) {
	$columns = $config['columns'];
	if ($columns == array()) {
		return false;
	}
	foreach ($columns as $column) {
		if (isset($column['primary_key']) && $column['primary_key'] === true) {
			return $column['name'];
		}
	}
	return true;
}

function xrvel_scaffolding_friendly_name($str) {
	$str = str_replace('_', ' ', $str);
	return ucwords($str);
}

function xrvel_scaffolding_parse_info($arg) {
	$select = array();
	$column_info = array();
	$column_names = array();
	$join = array();
	$join_temp_tables = array();

	$mode = $arg['mode'];
	$table_name = $arg['table_name'];
	$xs_config = $arg['config'];

	$i = 0;
	foreach ($xs_config['columns'] as $c) {
		if (isset($c['additional_join']) && $c['additional_join'] === true) {// Additional join
			if (isset($c['friendly_name'])) {
			} else {
				$xs_config['columns'][$i]['friendly_name'] = xrvel_scaffolding_friendly_name(xrvel_scaffolding_get_column_name($c['name']));
			}
		} else if (!isset($c['load_from'])) {// Not from join, simple select
			if (isset($c['friendly_name'])) {
			} else {
				$xs_config['columns'][$i]['friendly_name'] = xrvel_scaffolding_friendly_name($c['name']);
			}
		} else {// Join
			if (isset($c['friendly_name'])) {
			} else {
				$xs_config['columns'][$i]['friendly_name'] = xrvel_scaffolding_friendly_name(xrvel_scaffolding_get_column_name($c['name']));
			}
			if (isset($c['load_from']['friendly_name'])) {
			} else {
				$xs_config['columns'][$i]['load_from']['friendly_name'] = xrvel_scaffolding_friendly_name(xrvel_scaffolding_get_column_name($c['load_from']['display']));
			}
		}
		$i++;
	}

	//echo '<pre>';print_r($xs_config);echo '</pre>';

	foreach ($xs_config['columns'] as $c) {
		$proceed = true;
		if ($mode == 'view') {
			if (isset($c['hide_on']) && is_array($c['hide_on']) && $c['hide_on'] != array()) {
				if (in_array($mode, $c['hide_on'])) {
					$proceed = false;
				}
			}
		}
		if ($proceed) {
			if (isset($c['additional_join']) && $c['additional_join'] === true) {// Additional join
				$column_info[array_pop(explode('.', $c['name']))] = $c;
				$select[] = $c['name'];
				$column_names[] = $c['friendly_name'];
			} else if (!isset($c['load_from'])) {// Not from join, simple select
				$column_info[$c['name']] = $c;
				$select[] = $table_name.'.'.$c['name'];
				$column_names[] = $c['friendly_name'];
			} else {// Join
				if (!isset($c['skip_parent']) || (isset($c['skip_parent']) && $c['skip_parent'] === false)) {
					$select[] = $table_name.'.'.$c['name'];
					if (isset($c['friendly_name'])) {
						$column_names[] = $c['friendly_name'];
					} else {
						$column_names[] = xrvel_scaffolding_friendly_name($c['name']);
					}
				}

				$column_info[$c['name']] = $c;

				if (isset($c['load_from']['friendly_name'])) {
					$column_names[] = $c['load_from']['friendly_name'];
				} else {
					$column_names[] = xrvel_scaffolding_friendly_name($c['load_from']['display']);
				}

				$select[] = $c['load_from']['table'].'.'.$c['load_from']['display'];
				if (!isset($join_temp_tables[$c['load_from']['table']])) {
					$join_temp_tables[$c['load_from']['table']] = true;

					// Default join column
					$join_column = $c['name'];
					if (isset($c['load_from']['column'])) {
						$join_column = $c['load_from']['column'];
					}

					$join[] = $c['load_from']['join_type']." ".$c['load_from']['table']." ON $table_name.".$c['name'].' = '.$c['load_from']['table'].'.'.$join_column;
				}
			}
		}
	}

	$return = array(
		'select' => $select,
		'column_info' => $column_info,
		'column_names' => $column_names,
		'join' => $join,
		'config' => $xs_config
	);
	return $return;
}

function xrvel_scaffolding_print_query($query) {
	echo '<pre>'.htmlentities($query).'</pre>';
}
?>